package cn.xiaopengstack.domain.trade.rule.settlement;

import cn.xiaopengstack.domain.trade.rule.factory.SettlementRuleFilterFactory;
import cn.xiaopengstack.domain.trade.model.entity.GroupBuyOrderEntity;
import cn.xiaopengstack.domain.trade.model.entity.MarketOrderEntity;
import cn.xiaopengstack.domain.trade.model.entity.SettlementRuleCommandEntity;
import cn.xiaopengstack.domain.trade.model.entity.SettlementRuleResponseEntity;
import cn.xiaopengstack.domain.trade.repository.ITradeRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author jiangyangang
 */
@Component
@Slf4j
public class EndNodeFilter extends AbstractSettlementLogicLink<SettlementRuleCommandEntity, SettlementRuleFilterFactory.DynamicContext, SettlementRuleResponseEntity>{

    public EndNodeFilter(ITradeRepository tradeRepository) {
        super(tradeRepository);
    }

    @Override
    public SettlementRuleResponseEntity apply(SettlementRuleCommandEntity request, SettlementRuleFilterFactory.DynamicContext dynamic) throws Exception {
        log.info("结算规则流。End节点开始");
        GroupBuyOrderEntity groupBuyOrderEntity = dynamic.getGroupBuyOrderEntity();
        MarketOrderEntity marketOrderEntity = dynamic.getMarketOrderEntity();

        return SettlementRuleResponseEntity.builder()
                .activityId(marketOrderEntity.getActivityId())
                .teamId(marketOrderEntity.getTeamId())
                .groupEndTime(groupBuyOrderEntity.getGroupEndTime())
                .groupStartTime(groupBuyOrderEntity.getGroupStartTime())
                .targetCount(groupBuyOrderEntity.getTargetCount())
                .lockCount(groupBuyOrderEntity.getLockCount())
                .completeCount(groupBuyOrderEntity.getCompleteCount())
                .status(groupBuyOrderEntity.getStatus())
                .orderId(marketOrderEntity.getOrderId())
                .build();
    }
}
